#include <stdio.h>
#include "Mutex.h"

Mutex::Mutex()
{
	m_hMutex = ::CreateMutex(NULL, FALSE, NULL);
}

Mutex::~Mutex()
{
	::CloseHandle(m_hMutex);
}

void Mutex::Lock()
{
	::WaitForSingleObject(m_hMutex, INFINITE);
}

void Mutex::Unlock()
{
	::ReleaseMutex(m_hMutex);
}

Lock::Lock(Mutex &mutex, bool use):m_Mutex(mutex), m_bIsUsed(use)
{
	if (m_bIsUsed)
	{
		m_Mutex.Lock();
	}
}

Lock::~Lock()
{
	if (m_bIsUsed)
	{
		m_Mutex.Unlock();
	}
}